package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsFacility;
import net.realsee.domain.wcs.data.bo.WcsFacilityBo;
import net.realsee.domain.wcs.data.vo.WcsFacilityVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsFacilityMapper;
import net.realsee.service.wcs.data.IWcsFacilityService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工厂Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsFacilityServiceImpl extends ServiceImpl<WcsFacilityMapper, WcsFacility> implements IWcsFacilityService {

    private final WcsFacilityMapper baseMapper;

    /**
     * 查询工厂
     */
    @Override
    public WcsFacilityVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工厂列表
     */
    @Override
    public TableDataInfo<WcsFacilityVo> selectVoPage(WcsFacilityBo bo, PageQuery pageQuery) {
        IPage<WcsFacilityVo> result = baseMapper.selectDataByPage(pageQuery.build(), bo);
//        LambdaQueryWrapper<WcsFacility> lqw = buildVoQueryWrapper(bo);
//        Page<WcsFacilityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工厂列表
     */
    @Override
    public List<WcsFacilityVo> selectVoList(WcsFacilityBo bo) {
        LambdaQueryWrapper<WcsFacility> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsFacility> buildVoQueryWrapper(WcsFacilityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsFacility> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsFacility::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsFacility::getFacility, bo.getFacility());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsFacility::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), WcsFacility::getCompany, bo.getCompany());
        lqw.eq(bo.getCalendarId() != null, WcsFacility::getCalendarId, bo.getCalendarId());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsFacility::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsFacility::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsFacility::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsFacility::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsFacility::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsFacility::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsFacility::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsFacility::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsFacility::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsFacility::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsFacility::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsFacility::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsFacility::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增工厂
     */
    @Override
    public Boolean insertByBo(WcsFacilityBo bo) {
        WcsFacility add = BeanUtil.toBean(bo, WcsFacility.class);
        return this.save(add);
    }

    /**
     * 修改工厂
     */
    @Override
    public Boolean updateByBo(WcsFacilityBo bo) {
        WcsFacility update = BeanUtil.toBean(bo, WcsFacility.class);
        return this.updateById(update);
    }

    /**
     * 批量删除工厂
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
